package com.jk.controller;

import com.jk.dto.TemUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author yzf
 * @version 1.0
 * @ClassName HelloSenderController
 * @program: yzf-test
 * @description: TODO
 * @date 2025/2/27 15:13
 */
@Api(tags = "消息生产者测试")
@Slf4j
@RestController
public class HelloSenderController {
    // yzf TODO 2025/3/10 rabbitMQ 生产者:

    @Autowired
    private AmqpTemplate amqpTemplate;
    @ApiOperation(value = "发送消息",httpMethod = "GET",notes = "发送信息")
    @GetMapping("/send")
    public String send(){
        for (int i = 0; i < 100; i++){
            String contrext = "hello" + new Date();
            System.out.println("发送的消息"+ contrext);
            amqpTemplate.convertAndSend("hello", contrext);

        }
        return "发送成功";
    }

    @ApiOperation(value = "发送对象消息",httpMethod = "POST",notes = "发送对象信息")
    @PostMapping("/sendObj")
    public String sendObj(@RequestBody TemUser temUser){
        log.info("send object:" + temUser.toString() );
        amqpTemplate.convertAndSend("object", temUser);
        return "发送成功";
    }
    @ApiOperation(value = "发送消息",httpMethod = "POST",notes = "发送信息")
    @PostMapping("/sendMessageExchange")
    public String sendMessageExchange(@RequestBody TemUser temUser){
        System.out.println("发送的消息:"+temUser);
        amqpTemplate.convertAndSend("exchange","topic.message",temUser);
        amqpTemplate.convertAndSend("exchange","topic.messages",temUser);
    return "发送成功";
    }


    @ApiOperation(value = "广播发送",httpMethod = "POST",notes = "发送信息")
    @PostMapping("/sendMessageFanoutExchange")
    public String sendMessageFanoutExchange(@RequestBody TemUser temUser){
        System.out.println("发送的消息:"+temUser);
        amqpTemplate.convertAndSend("fanoutExchange","",temUser);
        return "发送成功";
    }
}
